<template>
  <div class="sub-category">
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <SubFilter @update-filter="updateFilter" />
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort @update-sort="updateSort" />
        <!-- 列表 -->
        <ul>
          <li v-for="item in list" :key="item.id">
            <GoodsItem :goods="item" />
          </li>
        </ul>
        <XtxInfiniteLoading :loading="loading" :finished="finished" @isLoading="onLoading" />
      </div>
    </div>
  </div>
</template>

<script>
import SubBread from './components/sub-bread'
import SubFilter from './components/sub-filter.vue'
import SubSort from './components/sub-sort.vue'
import GoodsItem from './components/goods-item.vue'
import { useRoute } from 'vue-router'
import { reactive, ref, watch } from 'vue'
import { findSubCategoryGoods } from '@/api/category'
export default {
  name: 'SubCategory',
  components: { SubBread, SubFilter, SubSort, GoodsItem },
  setup() {
    const route = useRoute()
    // 单次接口调用的状态
    const loading = ref(false)
    // 加载完成的标志
    const finished = ref(false)
    // 商品总数
    const total = ref(0)
    // 商品列表
    const list = ref([])
    // 请求参数
    const reqParams = reactive({
      id: route.params.id,
      page: 1,
      pageSize: 10
    })
    const onLoading = () => {
      loading.value = true
      console.log(loading.value)
      findSubCategoryGoods(reqParams).then(res => {
        list.value.push(...res.result.items)
        total.value = res.result.counts
        loading.value = false
        if (list.value.length === total.value) {
          // 没有更多数据了
          finished.value = true
        }
        // 页码需要累加
        reqParams.page += 1
      })
    }
    watch(
      () => route.params.id,
      newVal => {
        // 判断是否为二级分类
        if (route.fullPath !== '/category/sub/' + newVal) return
        // 重置数据
        reqParams.id = newVal
        list.value = []
        total.value = 0
        loading.value = false
        finished.value = false
        reqParams.page = 1
      }
    )
    const updateSort = val => {
      Object.assign(reqParams, val)
      console.log(reqParams)
      if (list.value.length === 0) {
        onLoading()
      } else {
        // 重置数据即可(只要数据置空，加载更多的组件就会进入可视区，那么就触发load数据)
        list.value = []
      }
      loading.value = false
      finished.value = false
      reqParams.page = 1
      total.value = 0
    }
    const updateFilter = val => {
      Object.assign(reqParams, val)
      // 重置数据即可(只要数据置空，加载更多的组件就会进入可视区，那么就触发load数据)
      if (list.value.length === 0) {
        onLoading()
      } else {
        // 重置数据即可(只要数据置空，加载更多的组件就会进入可视区，那么就触发load数据)
        list.value = []
      }
      reqParams.page = 1
      loading.value = false
      finished.value = false
      total.value = 0
    }
    return { onLoading, list, loading, finished, updateSort, updateFilter }
  }
}
</script>
<style scoped lang="less">
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
